/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FileReadWrite;
import Points.*;
import java.awt.Color;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
 *
 * @author Avishai Kopeliansky 
 */
public class Read {
	PointContainer pc;
	Point p;
	int x , y;
        Color c;

        /**
         * Constructor
         */
        public Read(){
		pc = new PointContainer();
		x = y =0;
		p = new Point();
                c = Color.BLACK;
	}

        /**
         *
         * @param st String - File Name
         * @return PointContainer with the points from the XML File
         */
        @SuppressWarnings({ "static-access"})
		public PointContainer Reader(String st){
		try {
			File file = new File(st);
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();			
			NodeList nodeLst = doc.getElementsByTagName("Point");
			int r,g,b;
                        r=g=b=255;

			for (int s = 0; s < nodeLst.getLength(); s++) {

				Node fstNode = nodeLst.item(s);

				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
					Element xCord = (Element) fstNode;
					NodeList xItem = xCord.getElementsByTagName("Xcord");
					Element xVal = (Element) xItem.item(0);
					NodeList fstNm = xVal.getChildNodes();					
					x =((Integer)x).parseInt((((Node) fstNm.item(0)).getNodeValue().trim()));
					NodeList yCord = xCord.getElementsByTagName("Ycord");
					Element yItem = (Element) yCord.item(0);
					NodeList yVal = yItem.getChildNodes();					
					y =((Integer)y).parseInt((((Node) yVal.item(0)).getNodeValue().trim()));
                                     
                                        
                                        NodeList rColor = xCord.getElementsByTagName("rColor");
					Element rItem = (Element) rColor.item(0);
					NodeList rVal = rItem.getChildNodes();					
					r =((Integer)r).parseInt((((Node) rVal.item(0)).getNodeValue().trim()));
                                        
                                        NodeList gColor = xCord.getElementsByTagName("gColor");
					Element gItem = (Element) gColor.item(0);
					NodeList gVal = gItem.getChildNodes();					
					g =((Integer)g).parseInt((((Node) gVal.item(0)).getNodeValue().trim()));
                                        
                                        NodeList bColor = xCord.getElementsByTagName("bColor");
					Element bItem = (Element) bColor.item(0);
					NodeList bVal = bItem.getChildNodes();					
					b =((Integer)b).parseInt((((Node) bVal.item(0)).getNodeValue().trim()));
                                        c=new Color(r,g,b);
                                       	p = new Point(x,y,c);
					pc.add(p);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pc;
	}


}